﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Pager.Account
{
    public partial class Login : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            RegistrarScripts();
        }
        
        protected void LoginUser_LoginError(object sender, EventArgs e) {
            //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "CustomValidation", "<script type=\"text/javascript\">alert('" + ConfigurationManager.AppSettings["ErrorMessageLogin"] + "');</script>", false);
            Label lblMessage = (Label)AchaControle(this.Page, "lblMessage");
            lblMessage.Text = "Usuário ou senha inválido";
            TextBox txtSenha = (TextBox)AchaControle(this.Page, "Password");
            txtSenha.Text = "";
        }
        
        private Control AchaControle(Control root, string id) {
            if (root.ID == id) return root;
            foreach (Control c in root.Controls) {
                Control t = AchaControle(c, id);
                if (t != null) return t;
            }
            return null;
        }

        protected void UserName_TextChanged(object sender, EventArgs e) {
            Label lbl = (Label)AchaControle(this.Page, "lblMessage");
            lbl.Text = "";
        }

        protected void RegistrarScripts() {
            Label lblMessage = (Label)AchaControle(this, "lblMessage");
            TextBox txtUsuario = (TextBox)AchaControle(this, "UserName");
            TextBox txtSenha = (TextBox)AchaControle(this, "Password");
            txtUsuario.Attributes.Add("onfocus", "document.getElementById('" + lblMessage.ClientID + "').innerHTML = '';");
            txtSenha.Attributes.Add("onfocus", "document.getElementById('" + lblMessage.ClientID + "').innerHTML = '';");
        }
    }
}
